package com.tuyan.aixiang.component;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.minminaya.widget.GeneralRoundRelativeLayout;
import com.tuyan.aixiang.R;
import com.tuyan.aixiang.util.ConvertUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ControlShortcut extends RelativeLayout {

    public static final int MAX_SIZE = 134;

//    public static final int MIN_WIDTH = 78;

    public static final int MIN_HEIGHT = 62;

    public static final int MAX_ICON_SIZE = 52;

    public static final int MIN_ICON_SIZE = 26;

    public static final int GAP = 5;

    public boolean showMsgCount = false;

    @BindView(R.id.iv_icon)
    ImageView ivIcon;

    @BindView(R.id.tv_title)
    TextView tvTitle;

    @BindView(R.id.grl_container)
    GeneralRoundRelativeLayout grlContainer;

    @BindView(R.id.tv_dot)
    TextView tvDot;

    public ControlShortcut(Context context) {
        this(context, null);
    }

    public ControlShortcut(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ControlShortcut(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View contentView = LayoutInflater.from(context).inflate(R.layout.control_shortcut_item, this);
        ButterKnife.bind(contentView, this);
    }

    public ControlShortcut setup(int iconRes, int titleRes, String color) {
        ivIcon.setImageResource(iconRes);
        tvTitle.setText(titleRes);
        grlContainer.setBackgroundColor(Color.parseColor(color));
        return this;
    }

    public ControlShortcut max() {

        int width = ConvertUtil.dp2px(getContext(), MAX_SIZE);
        int height = ConvertUtil.dp2px(getContext(), MAX_SIZE);
//        int iconWidth = ConvertUtil.dp2px(getContext(), MAX_ICON_SIZE);
        int iconHeight = ConvertUtil.dp2px(getContext(), MAX_ICON_SIZE);
        LayoutParams lp = new LayoutParams(width, height);
        LayoutParams iconLp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, iconHeight);
        int m = ConvertUtil.dp2px(getContext(), GAP);

        lp.setMargins(m, m, m, m);
        int margin = ConvertUtil.dp2px(getContext(), 17);
        iconLp.setMargins(margin, margin, margin, margin);
        setLayoutParams(lp);
        ivIcon.setLayoutParams(iconLp);
        return this;
    }

    public ControlShortcut min() {
//        int width = ConvertUtil.dp2px(getContext(), MIN_WIDTH);
        int height = ConvertUtil.dp2px(getContext(), MIN_HEIGHT);
//        int iconWidth = ConvertUtil.dp2px(getContext(), MIN_ICON_SIZE);
        int iconHeight = ConvertUtil.dp2px(getContext(), MIN_ICON_SIZE);
        LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height);
        LayoutParams iconLp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, iconHeight);
        int m = ConvertUtil.dp2px(getContext(), GAP);
        iconLp.setMargins(m, m, m, m);

        lp.setMargins(m, m, m, m);
        setLayoutParams(lp);
        ivIcon.setLayoutParams(iconLp);
        return this;
    }

    public void badge(int count) {
        tvDot.setVisibility(INVISIBLE);
        if(showMsgCount) {
            if(count > 0) {
                tvDot.setVisibility(VISIBLE);
            }
        }
    }

    public void setShowMsgCount(boolean show) {
        this.showMsgCount = show;
    }
}
